from core.basehandler import BaseHandler
from core.room import Room
from core.support import set_cutscene, get_game
from core.action import Action
from items.iPhone import IPhone

class Stairs(Room):
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        self.add_item(IPhone())
        # build list of actions
        self.add_action(Up)
        self.add_action(Back)
    
    def get_description(self):
        if self.is_illuminated():
            desc = "The stairwell leading to 6th floor is filled with smoke. You cannot go any further without a mask, otherwise you will inhale too much smoke and die."
        else:
            desc = "You can't go downstairs, because you can see the fire in the atrium blocking the entrance.  You should take the stairwell to the 6th floor but it's too dark to proceed, since the fire knocked the lights out.  You need a flashlight first."
        return desc
    
    def get_image(self):
        if self.is_illuminated():
            return 'stairs.jpg'
        else:
            return 'basement.jpg'
    
    # report whether this room is illuminated
    # which can happen if the room or player has a lit lamp
    def is_illuminated(self):
        
        game = get_game()
        
        # is there a lit lamp in the room itself?
        if 'flashlight' in self.inventory:
            if self.inventory['flashlight'].is_on:
                return True
        
        # does the player have a lit lamp?
        player = game.get_player()
        if 'flashlight' in player.inventory:
            if player.inventory['flashlight'].is_on:
                return True
                
        # if we get here, the lamp is not lit
        return False


    def is_passable(self):
        
        game = get_game()
        
        # does the player have a lit lamp?
        player = game.get_player()
        if 'mask' in player.inventory:
            if player.inventory['mask'].is_on:
                return True
                
        # if we get here, the lamp is not lit
        return False
    
class StairsHandler(BaseHandler):
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return Stairs

    # returns template to be used to render room.
    def get_template(self):
        room = self.get_this_room()
        if room.is_illuminated():
            return 'room.html'
        else:
            return 'room.html'

# define actions for this room

class Up(Action):
    def __init__(self):
        Action.__init__(self, "Up")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Head up to the 6th floor"
    
    # set cutscene for when arrive in entrance_hall
    def do_action(self,game,room,request):
        set_cutscene("You made it to the 6th floor. You can feel the heat intensifying and need to make it past the flames to get to Professor Wyners office.")
        return
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'sixth_floor'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
    
    # enabled only if room is illuminated.
    def is_enabled(self):
        if self.room.is_illuminated() and self.room.is_passable():
            return True
        else:
            return False


class Back(Action):
    def __init__(self):
        Action.__init__(self, "Back")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Return to the grad lounge"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'grad_lounge'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
    
